from selenium import webdriver
class BasePage:
    def __init__(self, driver=None):
        if driver:
            self.driver = driver
        else:
            self.driver = webdriver.Chrome()
            self.driver.implicitly_wait(5)
            self.driver.maximize_window()
# 实现 元素定位方式的 分割  "id , su"
    def converter(self, target):
        id, value = target.split(",")
        return id.strip(), value.strip()
    #    (XPATH "/ID=SU")
    def locate_element(self, target):
        id, value = self.converter(target)
        return self.driver.find_element(id, value)
    def input(self, target, keyvalue):
        self.locate_element( target).send_keys(keyvalue)

    def click(self, target):
        self.locate_element( target).click()

    def open(self, url, timeout=5):
        self.driver.get(url)

    def get_text(self, target):
        return self.locate_element( target).text

    def get_attribute(self, target, param):

        return self.locate_element( target).get_attribute(param)

    def quit(self):
        self.driver.quit()

class LoginPage(BasePage):
    def __init__(self,driver):
        super().__init__(driver)
        #
        self.account_loc ="xpath,//input[@id='account']"
        self.password_loc = "xpath,//input[@id='password']"
        self.submit_loc = "xpath, //button[@id='submit']"
        self.aasert_text_loc = "xpath,/html/body/div[2]/div/div/div[1]/div"
    def type_account(self,username):
        self.input(self.account_loc,username)

    def type_password(self,password):
        self.input(self.password_loc,password)

    def type_submit(self):
        self.click(self.submit_loc)

    # 返回登录失败 文本，作为断言失败的
    def assert_text(self):
        return self.get_text(self.aasert_text_loc)
    # 登录流程
    def test_login(self,username="admin",password="123456",expect="登录失败，请检查您的成员名或密码是否填写正确。"):
        self.open("http://localhost/ranzhi/www/" ,5)
        self.type_account(username)
        self.type_password(password)
        self.type_submit()
        self.wait(1)
        # print(self.assert_text())
        assert self.assert_text() == expect
        self.quit()

if __name__ == '__main__':
    driver = webdriver.Chrome()
    login = LoginPage(driver)
    login.test_login()